package com.nathan.crm.service;

import com.nathan.base.BaseService;
import com.nathan.crm.dao.ModuleMapper;
import com.nathan.crm.dao.PermissionMapper;
import com.nathan.crm.model.TreeModel;
import com.nathan.crm.vo.Module;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ModuleService extends BaseService<Module,Integer> {

    @Resource
    private ModuleMapper moduleMapper;
    @Resource
    private PermissionMapper permissionMapper;

    /**
     * 查询所有的模块以zTree的格式存储
     * @return
     */
    public List<TreeModel> queryAllModules(Integer roleId){
        // 先查询所有的资源
        List<TreeModel> treeModelList = moduleMapper.queryAllModules();

        // 根据角色id查询其对应的所拥有的资源id
        List<Integer> mIds = permissionMapper.queryRoleHasModuleIdByRoleId(roleId);

        // 判断是否有对应的资源
        if (mIds != null && mIds.size() > 0){
            // 循环所有的资源，获得每个TreeModel,判断mIds中是否包含TreeModel中的moduleId
            treeModelList.forEach(treeModel -> {
                if (mIds.contains(treeModel.getId())){
                    treeModel.setChecked(true);
                }
            });
        }

        return treeModelList;
    }

}
